<?php

namespace app\api\controller;
use think\Db;
use app\common\model\Banner as BannerModel;
use app\common\model\Search as SearchModel;
use app\common\model\User as UserModel;
use app\common\model\Article as ArticleModel;
class Index extends \app\api\controller\Base {

    /*====自动定位Start=====*/
    /**
     * location 自动定位返回id
     * $location  地区名称
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数组
     */
    public function location(){
        extract(input());
        $res = array();
        if(isset($sheng)){
            $shengID = Db::table('up_area')
                ->where('district',$sheng)
                ->value('district_id');
            $res['sheng'] = $shengID;
        }
        if(isset($shi)){
            $shiID = Db::table('up_area')
                ->where('district',$shi)
                ->value('district_id');
            $res['shi'] = $shiID;
        }
        if(isset($qu)){
            $quID = Db::table('up_area')
                ->where('district',$qu)
                ->value('district_id');
            $res['qu'] = $quID;
        }
        return json_encode(['status'=>200,'msg'=>$res]);
    }

    /**
     * 获取所有
     * @return string
     */
    public function getCitys(){
        header("Content-type:text/html;charset=utf-8");
        $res = Db::table('up_area')
            ->where('level',2)
            ->field('district_id id,district city')
            ->select();
        return json_encode(['status'=>200,'msg'=>$res]);
    }

    /*====自动定位End=====*/
    /**
     * 首页banner
     */
    public function banner(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $BannerModel = new BannerModel();
        $field='banner_id,banner_img,banner_sort,banner_link';
        $list = $BannerModel->selectBanner($field);
        $this->apiSuccess($list);
    }

    /***
     * 轮播详情
     */
    public function BannerDetail(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $id = input('post.banner_id');
        if(!$id){
            $this->apiError('E905');
        }else{
            $BannerModel = new BannerModel();
            $field='banner_id,banner_img,banner_link,banner_content';
            $list = $BannerModel->getOne($id,$field);
            if($list['banner_id']==''){
                $this->apiError('E905');
            }else{
                $this->apiSuccess($list);
            }
        }
    }

    /**
     * 用户的历史搜索记录
     */
    public function search(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $UserModel  = new  UserModel();
        $user = $UserModel->selectUser($user_id);

        if(!$user_id){$this->apiError('E905');}

        if(!$user){$this->apiError('L001');}

        $SearchModel = new SearchModel();

        $list = $SearchModel->History($user_id);

        $this->apiSuccess($list);
    }

    /***
     * 清除用户的搜索历史记录
     */
    public function clear(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
//        $UserModel  = new  UserModel();
//        $user = $UserModel->selectUser($user_id);
//
//        //判断用户是否存在
//        if(!$user){
//            $this->apiError('L001');
//        }
        $SearchModel = new SearchModel();
        $list = $SearchModel->clear($user_id);
        if($list){
            $this->apiSuccess();
        }else{
            $this->apiError('E001');
        }
    }

    /**
     * 搜索
     */
    public function SearchList()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $SearchModel = new SearchModel();
        $ArticleModel = new ArticleModel();
        $keyword = input('post.keyword');
        $city_id = input('post.city');
        $sheng = input('post.sheng');
//        $keyword = 11111;
//
//        $city_id = 220;
//        $sheng = 16;
        $uid = $user_id;
//        $uid = 18;
        $page = input('post.page')?input('post.page'):1;
        $type = input('post.type');
        $size = input('post.size');
        $startTime = date('Y-m-d H:i:s',strtotime('-1 day'));
        if($keyword!=''){//证明是搜索
            $whereMap['search_user_id']  = $uid;
            $whereMap['search_content']  = $keyword;
            //查询这个用户的搜索标题是否已经存在了
            $res = $SearchModel->selectOne($whereMap);
            if(!$res){//如果不存在就直接插入书库
                $data['search_content'] = $keyword;
                $data['search_user_id'] = $uid;
                $data['search_datetime'] = date('Y-m-d H:i:s');
                $SearchModel->addList($data);
            }
            $where['a.a_title'] = array('like',"%$keyword%");//搜索条件
        }
        if($type!=5&&$type!=''){//1是帮帮 2是走失 3福利  4悬赏'
            $where['a.a_type'] = $type;
        }else if($type==5){//热门
            $where['a.a_num'] = array('>',100);
        }
        $field ='a.a_type,a.a_image,a.a_auth_type,a.a_createtime,a.a_id,a.a_title,a.a_num,uuu.user_username,uuu.user_image';
        $where['a.a_createtime'] =array('>',$startTime);
        $where['a.a_del'] = 1;
        $where['a.a_down'] = 1;
        $list = $ArticleModel->selectArticleList($page,$field,$where,$size,$sheng,$city_id);
        foreach ($list as $key=>$vo){
            $vo['img'] = explode(',',$vo['a_image'])[0];
            unset($vo['a_image']);
        }
        $userData['user_last_login'] = date('Y-m-d H:i:s',time());

        $user_data = Db::table('up_u_user')->where('user_id',$uid)->find();
        if($user_data['user_sheng']==''&&$user_data['user_shi']==''){
            $userData['user_sheng'] =$sheng;
            $userData['user_shi'] = $city_id;
            Db::table('up_u_user')->where('user_id',$uid)->update($userData);
         }else{
            Db::table('up_u_user')->where('user_id',$uid)->update($userData);
        }
        $this->apiSuccess($list);
    }

    /***
     * 消息详情列表
     */
    public  function MessageDetail(){
        header("Access-Control-Allow-Origin:*");
        extract(input());
        if(!isset($share)){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == false){
                $this->apiError(301);
            }
        }

        $id = input('post.id');
        $uid = $user_id;
        if(!$id){
            $this->apiError('E905');
        }else{
            $a_user_id = Db::table('up_article')->where('a_id',$id)->value('a_user_id');
            if($a_user_id!=$uid){
                Db::table('up_article')->where('a_id',$id)->setInc('a_num',1);
            }
            $field =
            'a.a_sheng,a.a_shi,a.a_auth_city,a.a_auth_type,a.a_image,a.a_content,
            a.a_createtime,a.a_id,a.a_title,a.a_phone,
            a.a_num,uuu.user_username,uuu.user_phone,uuu.user_id';
            $a_auth_type = Db::table('up_article')->where('a_id',$id)->value('a_auth_type');
            if($a_auth_type!=1){//代表是精准发布

                $as_read_users = Db::table('up_article_son')->where('as_article_id',$id)->value('as_read_users');
                if($as_read_users!=''){//不为空
                    Db::startTrans();
                    $user_data = explode(',',trim($as_read_users,','));
                    if(in_array($uid,$user_data)==false){//证明不在数组中
                        $value =  ','.implode(',',$user_data).','.$uid.',';
                        $where1['as_article_id'] = $id;
                        $data['as_read_users'] = $value;
                        // 启动事务
                        $res = Db::table('up_article_son')->where($where1)->update($data);
                        if($res){
                            Db::commit();
                        }else{
                            Db::rollback();
                            $this->apiError('E000');
                        }
                    }
                }else{
                    $where1['as_article_id'] = $id;
                    $data['as_read_users'] = ','.$uid.',';
                    $res = Db::table('up_article_son')->where($where1)->update($data);
                    if($res){
                        Db::commit();
                    }else{
                        Db::rollback();
                        $this->apiError('E000');
                    }
                }
            }

            $where['a.a_id'] = $id;
            $ArticleModel = new ArticleModel();
            $list = $ArticleModel->selectArticleOne($field,$where);
            $district_s = Db::table('up_area')->where('district_id',$list['a_sheng'])->value('district');
            $district_c = Db::table('up_area')->where('district_id',$list['a_shi'])->value('district');
            $list['province'] = $district_s;
            $list['city'] = $district_c;
            if($list['a_auth_type']!=1){//精准发布
                if($list['a_auth_city']!=0){//不是金牌用户
                    $district = Db::table('up_area')->where('district_id',$list['a_auth_city'])->value('district');
                    $list['address'] = $district;
                }else{//金牌用户
                    $list['address'] = '全国';
                }
            }else{
                $list['address'] = '';
            }
            $this->apiSuccess($list);
        }
    }

    /***
     * 举报接口
     */
    public function report(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $uid = input('post.uid');//被举报人
        $id = input('post.id'); //消息ID
        $report_aid  = $user_id;//举报人
        $type = input('post.type');
        $content = input('post.content');//举报内容
        if($uid==''||$id==''||$report_aid==''||$type==''||$content==''){
            $this->apiError('E908');
            }
        // 启动事务
        Db::startTrans();
        $data['report_time'] = date('Y-m-d H:i:s',time());
        $data['report_user_id'] = $report_aid;
        $data['report_cover_user_id'] = $uid;
        $data['report_aid'] = $id;
        $data['report_type'] = $type;
        $data['report_reason'] = $content;
        $res = Db::table('up_report')->insert($data);
        if($res){
            Db::commit();
            $this->apiSuccess();
        }else{
            Db::rollback();
            $this->apiError('E000');
        }
    }

    /**
     * 举报类型接口
     */
    public function ReportType(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $where['report_status'] = 1;
        $res = Db::table('up_report_name')->where($where)->order('report_order desc')->select();
        $this->apiSuccess($res);
    }
}